<?php
class number{
    //非递归
    public function str_num($arr,$a){
        $coun =count($arr);
        $start = 0;
        while($start<=$coun){
            //向下取整
            $min = floor(($start+$coun)/2);
            if($arr[$min]==$a){
                return $min;
            }elseif ($arr[$min]<$a){
                $start = $min+1;
            }else{
                $coun = $min-1;
            }
        }
    }

    //递归
    public function str_nums($arr,$count,$start,$a){
        while($start<=$count){
            $min = floor(($start+$count)/2);
            if($arr[$min]==$a){
                return $min;
            }elseif ($arr[$min]<$a){
                //循环调用
                return $this->str_nums($arr,$count,$min+1,$a);
            }else{
                return $this->str_nums($arr,$min-1,0,$a);
            }
        }
    }
}
$arr = [1,2,3,4,5,6,7];
$data = new number;
print_r($data->str_num($arr,1));
echo "<br>";
print_r($data->str_nums($arr,count($arr),0,2));
?>